using System;
using System.Collections;
using System.Data;
//using PCS's namespaces
using PCSComUtils.PCSExc;
using PCSComUtils.Common;
using PCSComUtils.MasterSetup.DS;
using PCSComProcurement.Purchase.DS;
using PCSComUtils.DataContext;
using System.Collections.Generic;
using System.Transactions;
using PCSComUtils.DataAccess;
using System.Linq;

namespace PCSComProcurement.Purchase.BO
{
    public class POInvoiceBO //: IPOInvoiceBO
    {
        private const string THIS = "PCSComProcurement.Purchase.BO.POInvoiceBO";
        public POInvoiceBO()
        {
        }

        //[AutoComplete(true)]
        public void Add(object pObjectDetail)
        {
            throw new NotImplementedException();
        }
        //[AutoComplete(true)]
        public bool Insert(PO_InvoiceMaster pvoInvoiceMaster, List<PO_InvoiceDetail> listDetail)
        {
            bool ret = true;
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {
                        db.PO_InvoiceMasters.InsertOnSubmit(pvoInvoiceMaster);
                        db.SubmitChanges();
                        foreach (PO_InvoiceDetail objDetail in listDetail)
                            objDetail.InvoiceMasterID = pvoInvoiceMaster.InvoiceMasterID;
                        db.PO_InvoiceDetails.InsertAllOnSubmit(listDetail);
                        db.SubmitChanges();
                    }
                    catch (Exception ex)
                    {
                        ret = false;
                        Transaction.Current.Rollback();
                        throw ex;
                    }
                    finally { trans.Complete(); }
                    return ret;
                }
            }
        }
        public bool Update(PO_InvoiceMaster pvoInvoiceMaster, List<PO_InvoiceDetail> listDetail)
        {
            bool ret = true;
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {
                        var objMaster = db.PO_InvoiceMasters.SingleOrDefault(e => e.InvoiceMasterID == pvoInvoiceMaster.InvoiceMasterID);
                        objMaster.BLDate = pvoInvoiceMaster.BLDate;
                        objMaster.BLNumber = pvoInvoiceMaster.BLNumber;
                        objMaster.CarrierID = pvoInvoiceMaster.CarrierID;
                        objMaster.CCNID = pvoInvoiceMaster.CCNID;
                        objMaster.CurrencyID = pvoInvoiceMaster.CurrencyID;
                        objMaster.DeclarationDate = pvoInvoiceMaster.DeclarationDate;
                        objMaster.DeliveryTermID = pvoInvoiceMaster.DeliveryTermID;
                        objMaster.ExchangeRate = pvoInvoiceMaster.ExchangeRate;
                        objMaster.InformDate = pvoInvoiceMaster.InformDate;
                        objMaster.InvoiceNo = pvoInvoiceMaster.InvoiceNo;
                        objMaster.LastChange = pvoInvoiceMaster.LastChange;
                        objMaster.PartyID = pvoInvoiceMaster.PartyID;
                        objMaster.PaymentTermID = pvoInvoiceMaster.PaymentTermID;
                        objMaster.PostDate = pvoInvoiceMaster.PostDate;
                        objMaster.TaxDeclarationNumber = pvoInvoiceMaster.TaxDeclarationNumber;
                        objMaster.TaxInformNumber = pvoInvoiceMaster.TaxInformNumber;
                        objMaster.TotalBeforeVATAmount = pvoInvoiceMaster.TotalBeforeVATAmount;
                        objMaster.TotalCIFAmount = pvoInvoiceMaster.TotalCIFAmount;
                        objMaster.TotalCIPAmount = pvoInvoiceMaster.TotalCIPAmount;
                        objMaster.TotalImportTax = pvoInvoiceMaster.TotalImportTax;
                        objMaster.TotalInlandAmount = pvoInvoiceMaster.TotalInlandAmount;
                        objMaster.TotalVATAmount = pvoInvoiceMaster.TotalVATAmount;
                        objMaster.UserName = pvoInvoiceMaster.UserName;
                        db.SubmitChanges();
                        var list = db.PO_InvoiceDetails.Select(e => e.InvoiceMasterID == pvoInvoiceMaster.InvoiceMasterID).ToList();
                        //db.PO_InvoiceDetails.DeleteOnSubmit(list);                        

                        foreach (PO_InvoiceDetail objDetail in listDetail)
                            objDetail.InvoiceMasterID = pvoInvoiceMaster.InvoiceMasterID;
                        db.PO_InvoiceDetails.InsertAllOnSubmit(listDetail);
                        db.SubmitChanges();
                    }
                    catch (Exception ex)
                    {
                        ret = false;
                        Transaction.Current.Rollback();
                        throw ex;
                    }
                    finally { trans.Complete(); }
                    return ret;
                }
            }
        }
        public int AddAndReturn(object pvoInvoiceMaster, DataSet pdstDetail)
        {
            try
            {
                PO_InvoiceMasterDS dsMaster = new PO_InvoiceMasterDS();
                int intPOInvoiceMasterID = dsMaster.AddAndReturnID(pvoInvoiceMaster);
                foreach (DataRow objRow in pdstDetail.Tables[0].Rows)
                {
                    if (objRow.RowState == DataRowState.Deleted) continue;
                    objRow[PO_InvoiceDetailTable.INVOICEMASTERID_FLD] = intPOInvoiceMasterID;
                }

                PO_InvoiceDetailDS dsDetail = new PO_InvoiceDetailDS();
                dsDetail.UpdateDataSet(pdstDetail);
                return intPOInvoiceMasterID;
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //[AutoComplete(true)]
        public void Delete(object pObjectVO)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Delete record by condition
        /// </summary>
        //[AutoComplete(true)]
        public void Delete(object pobjMasterVO, DataSet pdtsDetail)
        {
            try
            {
                //First, delete data on detail table
                PO_InvoiceDetailDS dsDetail = new PO_InvoiceDetailDS();

                foreach (DataRow drow in pdtsDetail.Tables[0].Rows)
                {
                    //delete row
                    drow.Delete();
                }

                dsDetail.UpdateDataSet(pdtsDetail);

                //Then delete data on master table
                PO_InvoiceMasterDS dsMaster = new PO_InvoiceMasterDS();
                dsMaster.Delete(((PO_InvoiceMasterVO)pobjMasterVO).InvoiceMasterID);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        //[AutoComplete(true)]
        public object GetObjectVO(int pintID, string VOclass)
        {
            try
            {
                return (new PO_InvoiceMasterDS()).GetObjectVO(pintID);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        //[AutoComplete(true)]
        public object GetObjectVO(string pstrInvoiceNo)
        {
            try
            {
                return (new PO_InvoiceMasterDS()).GetObjectVO(pstrInvoiceNo);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        //[AutoComplete(true)]
        public void Update(object pObjectDetail)
        {
            throw new NotImplementedException();
        }

        //[AutoComplete(true)]
        public void UpdateDataSet(DataSet dstData)
        {
            throw new NotImplementedException();
        }


        /// <summary>
        /// CheckIfInvoiceHasBeenReceipt
        /// </summary>
        /// <param name="pintInvoiceDetailID"></param>
        /// <returns></returns>
        /// <author>Trada</author>
        /// <date>Thursday, September 28 2006</date>
        //[AutoComplete(true)]
        public bool CheckIfInvoiceHasBeenReceipt(int pintInvoiceDetailID)
        {
            PO_PurchaseOrderReceiptDetailDS dsPurchaseOrderReceiptDetail = new PO_PurchaseOrderReceiptDetailDS();
            return dsPurchaseOrderReceiptDetail.IsInvoiceHasBeenReceipt(pintInvoiceDetailID);
        }
        //[AutoComplete(true)]
        public DataTable SelectPO4Invoice(Hashtable mhtbCondition)
        {
            return (new PO_InvoiceMasterDS()).SelectPO4Invoice(mhtbCondition);
        }

        //[AutoComplete(true)]
        public DateTime GetEarliestApprovedDate(string pstrPODetailIDs)
        {
            try
            {
                return (new PO_InvoiceMasterDS()).GetEarliestApprovedDate(pstrPODetailIDs);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// GetDetailByMaster into Database
        /// </summary>
        //[AutoComplete(true)]
        public DataSet GetDetailByMaster(int pintMasterId)
        {
            return (new PO_InvoiceDetailDS()).GetDetailByMaster(pintMasterId);
        }

        /// <summary>
        /// Update into Database
        /// </summary>
        /// <param name="pobjMaster"></param>
        /// <param name="pdtbDetail"></param>
        //[AutoComplete(true)]
        public void Update(object pobjMaster, DataSet pdtsDetail)
        {
            PO_InvoiceMasterDS dsMaster = new PO_InvoiceMasterDS();
            dsMaster.Update(pobjMaster);

            //check and update master id for new added rows
            if (pdtsDetail != null)
            {
                foreach (DataRow row in pdtsDetail.Tables[0].Rows)
                {
                    if (row.RowState == DataRowState.Deleted) continue;
                    row[PO_InvoiceDetailTable.INVOICEMASTERID_FLD] = ((PO_InvoiceMasterVO)pobjMaster).InvoiceMasterID;
                }
            }

            PO_InvoiceDetailDS dsDetail = new PO_InvoiceDetailDS();
            //Check dataset to call UpdateDataSet method
            dsDetail.UpdateDataSet(pdtsDetail);
        }

        //[AutoComplete(true)]
        public object GetExchangeRate(int pintCurrencyID, DateTime pdtmOrderDate)
        {
            try
            {
                return (new MST_ExchangeRateDS()).GetExchangeRate(pintCurrencyID, pdtmOrderDate);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
    }
}